/**
 * Copyright &copy; 2012-2014 <a href="https://github.com.jims.emr">EMR</a> All rights reserved.
 */
package com.jims.master.modules.patient.myRecord.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.modules.patient.myRecord.dao.CaseDiagnosisDao;
import com.jims.master.modules.patient.myRecord.entity.CaseDiagnosis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于病历同步中诊断Service
 * @author xueyx
 * @version 2016-07-06
 */
@Service
@Transactional(readOnly = true)
public class CaseDiagnosisService extends CrudService<CaseDiagnosisDao, CaseDiagnosis> {
    @Autowired
    CaseDiagnosisDao emrDiagnosisDao;

    /**
     * 保存诊断
     * @param emrDiagnosis
     */
	@Transactional(readOnly = false)
	public void saveDeathDiag(CaseDiagnosis emrDiagnosis){
     List<CaseDiagnosis> list=emrDiagnosis.getEmrDiagnosisList();
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
                CaseDiagnosis diagnosis=list.get(i);
				this.save(diagnosis);
			}
		}

	}
	/**
	 * 通过父级Id查找诊断结果集
	 * @param parentId
	 * @return
	 */
	@Transactional(readOnly = false)
	public  List<CaseDiagnosis> findAllListByParent(String parentId){
		return emrDiagnosisDao.findAllListByParent(parentId);

	}
    @Transactional(readOnly = false)
    public  void batchSave(String id,List<CaseDiagnosis> emrDiagnosisList){
        if(emrDiagnosisList!=null){
            for (int i = 0; i < emrDiagnosisList.size(); i++) {
                CaseDiagnosis e=emrDiagnosisList.get(i);
                if(StringUtils.isNotEmpty(e.getDiagnosisId())){
                    e.setParentId(id);
                    this.save(e);
                }
            }
        }
    }
  public List<CaseDiagnosis> getAllDiag(String parentId){
      List<CaseDiagnosis> emrDiagnosises =new ArrayList<CaseDiagnosis>();
      CaseDiagnosis diagnosis = new CaseDiagnosis();
      List<CaseDiagnosis>  list=  emrDiagnosisDao.findAllDiagby(parentId);
      for(int i=0;i<list.size();i++){
          diagnosis =list.get(i);
         String type=  diagnosis.getType();
         List<CaseDiagnosis> emrDiagnosisList =  emrDiagnosisDao.findAllListByType(parentId, type);
         diagnosis.setEmrDiagnosisList(emrDiagnosisList);
          emrDiagnosises.add(diagnosis);
      }

      return emrDiagnosises;
  }
    /**
     * 通过父级Id查找诊断结果集(同步数据功能)
     * @param
     * @return
     */
    public List<CaseDiagnosis> findListByParent( String parent){
        return dao.findListByParent(parent);
    }
}